from core.celery import celery_app
from core.logger import logger
from time import sleep
from common.database import SessionLocal
from app.auth.models import AuthUsers


# Create logger - enable to display messages on task logger


# Create Order - Run Asynchronously with celery
# Example process of long running task
@celery_app.task(bind=True)
def create_order(self, name, quantity):


    # eg: 5 seconds
    session = SessionLocal()
    logger.info(AuthUsers.get_permissions_by_uuid(session,uuid=2))
    session.close()
    logger.info(self.request.id)
    complete_time_per_item = 5
    logger.info("123")
    # Keep increasing depending on item quantity being ordered
    sleep(complete_time_per_item * quantity)
    # Display Log
    logger.info(f"Order Complete!")
    return {"message": f"Hi {name}, Your order has completed!",
            "order_quantity": quantity}

# from typing import Optional
#
# from fastapi import BackgroundTasks, Depends, FastAPI
#
# app = FastAPI()
#
#
# def write_log(message: str):
#     with open("log.txt", mode="a") as log:
#         log.write(message)
#
#
# def get_query(background_tasks: BackgroundTasks, q: Optional[str] = None):
#     if q:
#         message = f"found query: {q}\n"
#         background_tasks.add_task(write_log, message)
#     return q
#
#
# @app.post("/send-notification/{email}")
# async def send_notification(
#     email: str, background_tasks: BackgroundTasks, q: str = Depends(get_query)
# ):
#     message = f"message to {email}\n"
#     background_tasks.add_task(write_log, message)
#     return {"message": "Message sent"}